﻿@page "/"
@page "/wasm"
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
@inject IAccessTokenProvider TokenProvider
@using System.Reflection
@inject NavigationManager Navigation

<PageTitle>Index</PageTitle>

<h1>Hello, wasm!</h1>

<div class="alert alert-warning" role="alert">
    TEST V @GetAppInfo()
</div>



<div style="width :500px">@output</div>

<button @onclick="DisplayToken">Display Access Token </button>

@code
{
    public string GetAppInfo() => $"{Assembly.GetExecutingAssembly().GetName().Version}";

    private string? output;

    private async Task DisplayToken()
    {
        var tokenResult = await TokenProvider.RequestAccessToken();

        if (tokenResult.TryGetToken(out var token))
        {
            output = token.Value;

        }
        else
        {
            output = "No token";
        }
    }

}



<p>AuthorizeView 组件根据用户是否获得授权来选择性地显示 UI 内容。 如果只需要为用户显示数据，而不需要在过程逻辑中使用用户的标识，那么此方法很有用</p>

<AuthorizeView>
    <Authorized>

        你好, @context.User.Identity?.Name

        @if (@context.User.IsInRole(AuthorizeRoles.Admin.ToString()))
        {
            <span>管理员</span>
        }
        else if (@context.User.IsInRole(AuthorizeRoles.Superuser.ToString()))
        {
            <span>超级用户</span>
        }
        else
        {
            <span>用户</span>
        }

        <button type="submit" class="nav-link btn btn-link" @onclick="BeginLogOut">注销</button>

    </Authorized>
    <NotAuthorized>
        <span>看起来你还没登录</span>
    </NotAuthorized>

</AuthorizeView>


<p>以下是基于角色或基于策略的授权,未登录不显示 </p>

<AuthorizeView Roles="Admin, Superuser">
    <p>只有管理员或超级用户才能看到.</p>
</AuthorizeView>

@code{
    private void BeginLogOut()
    {
        Navigation.NavigateToLogout("authentication/logout");
    }
}
